/**
 * 
 * @author Vanch
 * 
 */
;(function() {
	window.circle_pie = function(container_id, textType) {
		this.container_id = container_id;
		this.textType = textType;
	}
	
	//饼图初始化操作
	circle_pie.prototype.init = function() {
		var self = this;
		//玩法的颜色：#7bb851  #f15a23  #f7921e
		//面值的颜色：#f0a933  #a1305c  #5329cb
		var colorList=['#f7921e','#f15a23','#7bb851','#f15a23'];
		var dataLabels=[];
		var dataValues=[];
		var initcolor='#f7921e';
		
		var domPie = document.getElementById(self.container_id);
		var chartPie = echarts.init(domPie);
		self.chartPie = chartPie;
		
		self.zoom=(self.chartPie.getWidth()+self.chartPie.getHeight())/236;		
		
		//饼图参数配置，默认参数
		optionPie = {
		    backgroundColor:'transparent',
			name:'玩法占比',
		    tooltip: {
				show: false,
		    },
		    legend: {
		        show:false,
		    },
		    series: [
		        {
		            name:'玩法排名',
		            type:'pie',
		            radius: ['62.1%', '100%'],
		            avoidLabelOverlap: false,
		            animation:false,
		            hoverAnimation:false,
		            legendHoverLink:false,
					silent:true,
		            label: {
		                normal: {
		                    show: false,
		                    position: 'center'
		                },
		                emphasis: {
		                    show: true,
		                    textStyle: {
		                        fontSize: '30',
		                        fontWeight: 'bold'
		                    }
		                }
		            },
		            labelLine: {
		                normal: {
		                    show: false
		                }
		            },
		            data:[
		                {value:1, name:"默认饼图",itemStyle:{normal:{color:initcolor}}},
		            ]
		        }
		    ]
		};
		
		//设置饼图
		if (optionPie && typeof optionPie === "object") {
		    chartPie.setOption(optionPie, true);
		}
	};
	
	//饼图刷新操作
	circle_pie.prototype.refresh = function(colorList, dataLabels, dataValues) {
		var self = this;
		
		var text = "面值销量\n\n前三占比";
		var colorValue = '#f7921e';
		if (self.textType != "mianzhi"){	// mianzhi,wanfa
			text = "玩法销量\n\n前三占比";
			colorValue = '#f15a23';
		}
		
		//饼图默认参数
		//var dataLabels=['争分夺秒','百发百中','好运十倍'];
		//var dataValues=[335,135,1548];
		var pieData=[{value:1, name:text,itemStyle:{normal:{color:colorValue}}},{value:0, name:text,itemStyle:{normal:{color:'#FFFFFF'}},label:{normal:{show:true,textStyle:{fontSize:10}}}}];
		if (dataValues && dataValues.length && dataValues.length>3){
			//console.log("dataValues.length>2");
			if(typeof(dataLabels[0])=="undefined"){ 
				//console.log("if(typeof(dataLabels[0])==undefined){ ");
				dataLabels[0]="争分夺秒";
			} 
			if(typeof(dataLabels[1])=="undefined"){ 
				dataLabels[1]="百发百中";
			} 
			if(typeof(dataLabels[2])=="undefined"){ 
				dataLabels[2]="好运十倍";
			} 
			if(typeof(dataLabels[3])=="undefined"){ 
				dataLabels[3]="其他";
			} 
			
			if(typeof(dataValues[0])=="undefined"){ 
				//console.log("if(typeof(dataValues[0])==undefined){ ");
				dataValues[0]=0;
			} 
			if(typeof(dataValues[1])=="undefined"){ 
				dataValues[1]=0;
			} 
			if(typeof(dataValues[2])=="undefined"){ 
				dataValues[2]=0;
			} 
			if(typeof(dataValues[3])=="undefined"){ 
				dataValues[3]=0;
			} 
			
			
			if(dataValues[0]<0){ 
				//console.log("if(typeof(dataValues[0])<0){ ");
				dataValues[0]=0;
			} 
			if(dataValues[1]<0){ 
				dataValues[1]=0;
			} 
			if(dataValues[2]<0){ 
				dataValues[2]=0;
			} 
			if(dataValues[3]<0){ 
				dataValues[3]=0;
			} 

			
			if(!isFinite(dataValues[0])){ 
				//console.log("if(isFinite(dataValues[0])){ ");
				dataValues[0]=0;
			} 
			if(!isFinite(dataValues[1])){ 
				dataValues[1]=0;
			} 
			if(!isFinite(dataValues[2])){ 
				dataValues[2]=0;
			} 
			if(!isFinite(dataValues[3])){ 
				dataValues[3]=0;
			} 
			
			//console.log(dataValues);
			
			//alert((dataValues[0]+dataValues[1]+dataValues[1])==0);
			if((dataValues[0]+dataValues[1]+dataValues[1])!=0){
				pieData=[
						{value:dataValues[0], name:dataLabels[0],itemStyle:{normal:{color:colorList[0]}},label:{normal:{show:false}}},
						{value:dataValues[1], name:dataLabels[1],itemStyle:{normal:{color:colorList[1]}}},
						{value:dataValues[2], name:dataLabels[2],itemStyle:{normal:{color:colorList[2]}}},
						{value:dataValues[3], name:dataLabels[3],itemStyle:{normal:{color:colorList[3]}}},
						{value:0, name:text,itemStyle:{normal:{color:'#FFFFFF'}},label:{normal:{show:true,textStyle:{fontSize:10*self.zoom}}}},
					];
			}
		}
		//alert(self.zoom);
		
		//饼图参数设置
		var optionPie = {
			series: [{
				data:pieData,
				markPoint:{symbolSize:50,symbolOffset:[0,0],symbol:"image://http://softtree.cn/allcharts/img/time_dot_clicked.png"}
			}]
		};
		
		//更新饼图
		self.chartPie.setOption(optionPie);
		//console.log("refresh end..");
		//console.log(dataValues);
	};

})(Zepto);